package com.lyncs.ods.utils;

import com.alibaba.fastjson.JSONObject;
import com.lyncs.ods.modules.goods.model.SkuInfo;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * @author alex
 * @date 2022/4/26 00:32
 * @description
 */
public class SkuUtils {

    public static BigDecimal toCurrencyAmount(SkuInfo skuInfo, String currency) {
        if (currency == null) {
            return null;
        }
        if (skuInfo.getAmount() == null || currency.equals(skuInfo.getCurrency())) {
            return skuInfo.getAmount();
        }
        String amountJsonStr = skuInfo.getAmountJson();
        JSONObject amountJson = StringUtils.isNotEmpty(amountJsonStr) ? JSONObject.parseObject(amountJsonStr) : new JSONObject();
        if (amountJson.containsKey(currency)) {
            return amountJson.getBigDecimal(currency);
        }
        return CurrencyUtil.exchange(skuInfo.getCurrency(), currency, skuInfo.getAmount());
    }
}
